﻿using Network.UpdateData;
using System;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// 接收到客户端传送文件的请求, 向集群发送广播请求发送文件
    /// Packet: targetGuid, tarnsportGuid
    /// </summary>
    class PH05Transport : PacketHandle
    {
        public PH05Transport(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected override void Handle()
        {
            string[] str = Message.Split('♠');
            Guid targetGuid = new Guid(str[0]);
            Guid transGuid = new Guid(str[1]);
            bool hasXml = false;//等待xml文件写完
            for (int i = 0; i < 10; i++)
            {
                if (Runtime.xmlUpload.isThereGuid(transGuid))
                {
                    hasXml = true;
                    break;
                }
                else
                {
                    Delay(100 * i);
                }
            }
            if (!hasXml)
            {
                throw new Exception("错误：读取xml文档未找到指定任务 " + GetType());
            }
            bool find = false;//是否收到目标的回应
            for (int i = 0; i < 10; i++)
            {
                Launcher.SendMsg2One(typeof(PH05ReGotRequest).GetDictianaryKey(), targetGuid, transGuid.ToString());
                Delay(2000);
                while (Runtime.onSending)
                {
                    Delay(500);
                }
                for (int j = Runtime.transportSend.Count - 1; j >= 0; j--)
                {
                    if (Runtime.transportSend[j].Equals(transGuid))
                    {
                        Runtime.transportSend.Remove(Runtime.transportSend[j]);
                        find = true;
                    }
                }
                if (find)
                {
                    break;
                }
            }
        }
    }
}
